﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IceCS.Img2Base64
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private bool _firstOpen = true;

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件(*.jpg,*.png,*.ico)|*.jpg;*.png;*.ico";
            openFileDialog1.Multiselect = false;
            if (_firstOpen)
            {
                openFileDialog1.InitialDirectory = Path.Combine(Environment.CurrentDirectory, "test-files");
                _firstOpen = false;
            }
            else
            {
                openFileDialog1.InitialDirectory = "";
            }
            
            try
            {
                var showResult = openFileDialog1.ShowDialog();
                if (showResult == DialogResult.Cancel) { return; }
                var fileStream = openFileDialog1.OpenFile();
                var ms1 = new MemoryStream();
                var ms2 = new MemoryStream();
                fileStream.CopyTo(ms1);
                fileStream.Position = 0;
                fileStream.CopyTo(ms2);
                ms1.Position = 0;
                ms2.Position = 0;
                ms1.Seek(0, SeekOrigin.Begin);
                var text = Core.ConvertImgToBase64Text(ms2);
                textBox1.Text = text;
                pictureBox1.Image = new Bitmap(ms1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                var text = textBox1.Text;
                var stream = Core.ConvertBase64TextToImg(text);
                pictureBox1.Image = new Bitmap(stream);
            }
            catch (Exception ex)
            {
                pictureBox1.Image = null;
                MessageBox.Show(ex.Message);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
            textBox1.Copy();
            MessageBox.Show("复制成功");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var text = textBox1.Text;
            if (text.IndexOf("data:image/png;base64,") == -1)
            {
                text = "data:image/png;base64," + text;
            }
            textBox1.Text = text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var text = textBox1.Text;
            if (text.IndexOf("data:image/png;base64,") > -1)
            {
                text = text.Replace("data:image/png;base64,", "");
            }
            textBox1.Text = text;
        }
    }
}
